package com.xds.gulimall.auth.controller;

import com.alibaba.fastjson.TypeReference;
import com.xds.common.constant.AuthServerConstant;
import com.xds.common.utils.R;
import com.xds.common.vo.MemberResVo;
import com.xds.gulimall.auth.feign.MemberFeign;
import com.xds.gulimall.auth.feign.ThirdPartFeign;
import com.xds.gulimall.auth.vo.UserLoginVo;
import com.xds.gulimall.auth.vo.UserRegistVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author : baixu
 * @date : 18:09 2021/12/16
 */
@Controller
public class LoginController {
    @Autowired
    ThirdPartFeign thirdPartFeign;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    MemberFeign memberFeign;
    @ResponseBody
    @GetMapping("/sms/sendCode")
    public R sendCode(@RequestParam("phone")String phone){
        thirdPartFeign.sendMsm(phone);
        return R.ok();
    }
    @PostMapping("/regist")
    public String register(@Valid UserRegistVo vo, BindingResult result, RedirectAttributes redirectAttributes){
        if (result.hasErrors()){
            Map<String, String> errors = result.getFieldErrors().stream().collect(Collectors.toMap(fieldError -> {
                        return fieldError.getField();
                    },
                    fieldError -> {
                        return fieldError.getDefaultMessage();
                    }
            ));
//            model.addAttribute("errors",errors);
            redirectAttributes.addFlashAttribute("errors",errors);
            return "redirect:http://auth.xiaowenplus.com/reg.html";
        }
        //注册成功回到登陆页
         //1.校验验证码
        String code = vo.getCode();
        String redisCode = redisTemplate.opsForValue().get(vo.getPhone());
        if (!StringUtils.isEmpty(redisCode)){
            if (code.equals(redisCode)){
                //删除验证码
                redisTemplate.delete(vo.getPhone());
                //验证码通过
                R r = memberFeign.refist(vo);
                if (r.getCode()==0){
                    return "redirect:http://auth.xiaowenplus.com/login.html";
                }else {
                    Map<String, String> errors=new HashMap<>();
                    errors.put("msg",r.getData("msg",new TypeReference<String>(){}));
                    redirectAttributes.addFlashAttribute("errors",errors);
                    return "redirect:http://auth.xiaowenplus.com/reg.html";
                }

            }else {
                Map<String, String> errors=new HashMap<>();
                errors.put("code","验证码错误");
                redirectAttributes.addFlashAttribute("errors",errors);
                return "redirect:http://auth.xiaowenplus.com/reg.html";
            }
        }else {
            Map<String, String> errors=new HashMap<>();
            errors.put("code","验证码错误");
            redirectAttributes.addFlashAttribute("errors",errors);
            return "redirect:http://auth.xiaowenplus.com/reg.html";
        }


    }
    @PostMapping("/login")
    public String login(UserLoginVo vo, RedirectAttributes redirectAttributes, HttpSession session){
        //远程调用登陆
        R r = memberFeign.login(vo);
        if (r.getCode()==0){
            MemberResVo data = r.getData("data", new TypeReference<MemberResVo>() {
            });
            session.setAttribute(AuthServerConstant.LOGIN_USER,data);
            return "redirect:http://xiaowenplus.com";
        }else {
            Map<String, String> errors=new HashMap<>();
            errors.put("msg",r.getData("msg",new TypeReference<String>(){}));
            redirectAttributes.addFlashAttribute("errors",errors);
            return "redirect:http://auth.xiaowenplus.com/login.html";
        }

    }
}
